package net.cassandraview.db;

import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.Mutation;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;

/**
 * Class containing methods for querying/inserting/deleting data.
 *
 * Not thread safe. Should be used from one single thread only.
 *
 * Please be sure to call finish() method (better in finally block) to return underlying connection to manager's pool.
 *
 * User: nagakhl
 * Date: 21.01.12
 * Time: 21:37
 */
public interface Connection {

    /**
     * Set keyspace to use for queries
     * @param keyspace new keyspace name
     */
    public void setKeyspace(String keyspace);
    
    public String getKeyspace();
    
    public void setLimit(int limit);
    
    public int getLimit();

    public void setConsistencyLevel(ConsistencyLevel level);

    public ConsistencyLevel getConsistencyLevel();
    
    public List<KeySlice> get(String family, Condition range);

    /**
     * Execute CQL expression and retrieve results
     *
     * @param query CQL expression
     * @return Result of execution
     */
    public Result query(String query);

    /**
     * Insert data into column.
     *
     * @param rowKey RowKey to put values under
     * @param columnFamily Name of column family to insert data in
     * @param columns Values to insert
     */
    public void insert(String columnFamily, ByteBuffer rowKey, List<Column> columns);

    /**
     * Insert data into supercolumn
     *
     * @param superColumnFamily Column family of type super to insert into
     * @param rowKey RowKey to put values under
     * @param columns Data to insert
     */
    public void insert(String superColumnFamily, ByteBuffer rowKey, Map<ByteBuffer, List<Column>> columns);

    /**
     * Execute raw cassandra Mutations
     *
     * @param mutationMap Mutations to execute
     */
    public void mutate(Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap);

    public void finish();
    
}
